package cn.simon.controller;

import cn.simon.model.Goods;
import cn.simon.model.GoodsClassify;
import cn.simon.model.GoodsClassifySec;
import cn.simon.service.GoodsClassifySecService;
import cn.simon.service.GoodsClassifyService;
import cn.simon.service.GoodsService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 本控制器负责首页相关请求和响应
 *
 * @author Kinfer
 * @date 2022/10/24
 */
@Log
@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsClassifyService goodsClassifyService;
    @Autowired
    private GoodsClassifySecService goodsClassifySecService;

    /**
     * 响应首页跳转请求
     *
     * @param classifyId
     * @return
     * @author Kinfer
     * @date 2022/10/25
     */
    @GetMapping("")
    public String allGoods(Integer classifyId, Integer classifySecId) {
        if (classifySecId != null) {
            return "forward:/index/showAllGoodsByClassifySec?classifySecId=" + classifySecId
                    + "&classifyId=" + classifyId;
        } else if (classifyId != null) {
            return "forward:/index/showAllGoodsByClassify?classifyId=" + classifyId;
        } else {
            return "forward:/index/showAllGoodsOnShelf";
        }
    }

    /**
     * 响应首页搜索请求
     *
     * @param keyword
     * @return
     * @author Kinfer
     * @date 2022/10/27
     */
    @PostMapping("")
    public String allGoods(String keyword) {
        return "forward:/index/showAllGoodsByKeyword?"+keyword;
    }

    /**
     * 在首页显示所有上架商品
     *
     * @param model
     * @return
     * @author Kinfer
     * @date 2022/10/25
     */
    @GetMapping("/showAllGoodsOnShelf")
    public String showAllGoodsOnShelf(Model model) {
        List<GoodsClassify> goodsClassifyList = goodsClassifyService.getAllGoodsClassify();
        List<Goods> goodsList = goodsService.getAllGoodsOnShelf();

        model.addAttribute("goodsList", goodsList);
        model.addAttribute("goodsClassifyList", goodsClassifyList);

        return "index";
    }

    /**
     * 在首页显示一级分类下所有上架商品
     *
     * @param classifyId
     * @param model
     * @return
     * @author Kinfer
     * @date 2022/10/26
     */
    @GetMapping("/showAllGoodsByClassify")
    public String showAllGoodsByClassify(Integer classifyId, Model model) {
        List<GoodsClassify> goodsClassifyList = goodsClassifyService.getAllGoodsClassify();
        List<GoodsClassifySec> goodsClassifySecList = goodsClassifySecService.getAllGoodsClassifySec(classifyId);
        List<Goods> goodsList = goodsService.getAllGoodsByClassify(goodsClassifySecList);

        model.addAttribute("goodsList", goodsList);
        model.addAttribute("goodsClassifyList", goodsClassifyList);
        model.addAttribute("goodsClassifySecList", goodsClassifySecList);

        return "index";
    }

    /**
     * 在首页显示二级分类下所有上架商品
     *
     * @param classifyId
     * @param classifySecId
     * @param model
     * @return
     * @author Kinfer
     * @date 2022/10/26
     */
    @GetMapping("/showAllGoodsByClassifySec")
    public String showAllGoodsByClassifySec(Integer classifyId, Integer classifySecId, Model model) {
        List<GoodsClassify> goodsClassifyList = goodsClassifyService.getAllGoodsClassify();
        List<GoodsClassifySec> goodsClassifySecList = goodsClassifySecService.getAllGoodsClassifySec(classifyId);
        List<Goods> goodsList = goodsService.getAllGoodsByClassifySec(classifySecId);

        model.addAttribute("goodsList", goodsList);
        model.addAttribute("goodsClassifyList", goodsClassifyList);
        model.addAttribute("goodsClassifySecList", goodsClassifySecList);

        return "index";
    }

    /**
     * 在首页显示与关键字匹配的所有上架商品
     *
     * @param keyword
     * @param model
     * @return
     * @author Kinfer
     * @date 2022/10/27
     */
    @PostMapping("/showAllGoodsByKeyword")
    public String showAllGoodsByKeyword(String keyword, Model model) {
        List<GoodsClassify> goodsClassifyList = goodsClassifyService.getAllGoodsClassify();
        List<Goods> goodsList = goodsService.getAllGoodsByKeyword(keyword);

        model.addAttribute("goodsClassifyList", goodsClassifyList);
        model.addAttribute("goodsList", goodsList);

        return "index";
    }
}
